<?php
namespace app\msgapi\controller;

use app\msgapi\model\UserCust;
use app\msgapi\service\HuihuaSensitiveService;
use app\msgapi\service\IndexService;
use app\msgapi\service\WeworkCorpSelfService;
use app\Request;
use app\utils\Mail;
use PHPMailer\PHPMailer\PHPMailer;
use think\facade\Log;

class Index extends Base
{
    public function __construct(IndexService $service)
    {
        parent::__construct($service);
        $this->service = $service;
    }

    //菜单信息
    public function navlst(Request $request)
    {
        return $this->service->getMenu($request->admin_groupid);
    }

    //首页信息
    public function index(Request $request)
    {
        $data = $request->only(['time_type','start_time','end_time','userid','interface']);
        if($data['interface'] === 'statistics'){
            return $this->service->get_base_data($data);
        }
        if($data['interface'] === 'line_chart'){
            return $this->service->get_echats_data($data);
        }
        return error_json('参数错误');
    }

    //站点配置信息
    public function siteConfig()
    {
        return $this->service->getSystemAll();
    }
    // 更新站点配置
    public function upSiteConfig()
    {
        return $this->service->updateConfig();
    }
    // 修改密码
    public function changePassword(Request $request)
    {
        return $this->service->updatePassword($request->adminid);
    }

    //修改邮箱
    public function changeEmail(Request $request)
    {
        $data = $request->only(['email'], 'post');
        return $this->service->updateEmail($data, $request->adminid);
    }
    //清除缓存
    public function clearCache()
    {
        return $this->service->clearCache();
    }

    /**获取登入用户信息(手机端)
     * @param Request $request
     * @return \think\response\Json
     */
    public function getUserInfo(Request $request)
    {
        return $this->service->getUserInfo($request->adminid);
    }

    /**获取js签名信息
     * @param Request $request
     * @return \think\response\Json
     * @throws \Throwable
     */
    public function getJsTicketInfo(Request $request)
    {
        $data = $request->only(['url'],'post');
        return $this->service->getJsTicketInfo($data);
    }


    /**测试消息提醒功能
     * @return void
     * @throws \app\utils\UtilsException
     */
    public function sendTestMsg(){

        $weworkCorpSelf = new WeworkCorpSelfService();
        Log::write($weworkCorpSelf->sendTempMessage([env('testmsg.work_weixin')],'markdown','测试消息:收到该消息说明应用通知功能调试成功',0),'info');
        $PHPMailer = new PHPMailer();
        $mail = new Mail($PHPMailer);
        Log::write($mail->send( '测试邮件', '<p>'.'收到该邮件说明邮件提醒功能调试成功'.'</p>', [env('testmsg.msg_email')]),'info');
    }
}